[ruby] How to convert STDIN contents to an array?

Posted by miketaylr on Stack Overflow See other posts from Stack Overflow or by miketaylr
Published on 2009-02-14T03:49:47Z Indexed on 2010/04/21 7:23 UTC
Read the original article Hit count: 103

Filed under:
|
|

I've got a file INPUT that has the following contents:

123\n
456\n
789

I want to run my script like so: script.rb < INPUT and have it convert the contents of the INPUT file to an array, splitting on the new line character. So, I'd having something like myArray = [123,456,789]. Here's what I've tried to do and am not having much luck:

myArray = STDIN.to_s
myArray.split(/\n/)
puts field.size

I'm expecting this to print 3, but I'm getting 15. I'm really confused here. Any pointers?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about stdin